<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\energy;

use think\App;
use app\controller\property\Base;
use app\validate\energy\EnergyQrcodeValidate;
use app\common\repositories\energy\EnergyMeterRepository;
use app\common\repositories\energy\EnergyQrcodeRepository;


class EnergyQrcode extends Base
{
    protected $repository;

    public function __construct(App $app, EnergyQrcodeRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyEnergyQrcodeAdd'),
            'editAuth' => property_auth('propertyEnergyQrcodeEdit'),
            'delAuth' => property_auth('propertyEnergyQrcodeDel'),
        ]);
    }

    protected function commonParams()
    {
        /** @var EnergyMeterRepository $energyMeterRepository */
        $energyMeterRepository = app()->make(EnergyMeterRepository::class);
        $energyTypeData = $energyMeterRepository::ENERGY_TYPE;
        $this->assign([
            'energyTypeData' => $energyTypeData,
        ]);
    }


    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'energy_id' => '',
                'energy_type' => '',
                'keywords' => '',
                'is_bind' => '',
                'add_time' => ''
            ]);
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        }
        $this->commonParams();
        return $this->fetch('energy/qrcode/list');
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'prefix_no' => '',
                'num' => '',
                'start_num' => '',
                'remark' => '',
            ]);
            validate(EnergyQrcodeValidate::class)->scene('add')->check($param);
            $this->repository->addInfo($this->request->propertyId, $param, $this->request->userId);
            return $this->success('添加成功');
        } else {
            return $this->fetch('energy/qrcode/add', [
                'prefix_no' => 'ZG'
            ]);
        }
    }


    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'energy_type' => '',
                'energy_no' => '',
                'remark' => '',
            ]);
            validate(EnergyQrcodeValidate::class)->scene('edit')->check($param);
            if ($this->repository->fieldExists('energy_no', $param['energy_no'], $id)) {
                return $this->error($param['energy_no'] . '已存在');
            }
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                exception_log('修改设备二维码失败', $e);
                return $this->error('网络错误' . $e->getMessage());
            }
        } else {
            $this->commonParams();
            return $this->fetch('energy/qrcode/edit', [
                'info' => $info,
            ]);
        }
    }

    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            exception_log('删除设备二维码失败', $e);
            return $this->error('网络失败');
        }
    }
}